/* Copyright 2008, Arun Zaheeruddin (a.reginald@gmail.com)
 * 
 * Licensed under the GNU Lesser General Public License, Version 3.0 (the "License").
 * A copy of the License may be obtained from http://www.gnu.org/licenses/lgpl-3.0.txt
 * unless it is otherwise distributed with the source-code (the "Source").
 * Consider reviewing the License for further details about using this Source in your
 * applications and projects.
 *
 * @author Arun Zaheeruddin
 * @version 0.1.0.1
 */

package com.db2object.data;
import java.util.ArrayList;
public class DataTable
{
	private String name;
	private ArrayList<DataColumn> tableColumns;
	private DataColumn[] primaryKeyColumns;
	public DataTable()
	{
		this.name = null;
		this.tableColumns = new ArrayList<DataColumn>();
	}
	public DataTable(String name)
	{
		this();
		this.name = name;
	}
	public boolean addColumn(DataColumn column)
	{
		column.setTable(this);
		column.setOrdinal(this.tableColumns.size() + 1);
		return this.tableColumns.add(column);
	}
	public boolean removeColumn(DataColumn column)
	{
		return this.tableColumns.remove(column);
	}
	public void setColumns(DataColumn[] columns)
	{
		this.tableColumns.clear();
		for(DataColumn column : columns)
		{
			this.addColumn(column);
		}
	}
	public DataColumn[] getColumns()
	{
		if(this.tableColumns.size() == 0) return new DataColumn[0];
		DataColumn[] columnsArray = new DataColumn[this.tableColumns.size()];
		int count = 0;
		for(DataColumn column : this.tableColumns)
		{
			columnsArray[count++] = column;
		}
		return columnsArray;
	}
	public DataColumn getColumn(int index)
	{
		return this.getColumns()[index];
	}
	public void setPrimaryKey(DataColumn...columns)
	{
		this.primaryKeyColumns = columns;
	}
	public DataColumn[] getPrimaryKey()
	{
		return this.primaryKeyColumns;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public String getName()
	{
		return this.name;
	}
	public Object get(String identifier)
	{
		if(identifier.equalsIgnoreCase("COLUMNS"))
		{
			return this.getColumns();
		}
		else
		{
			return null;
		}
	}
	public Object get(String identifier, int index)
	{
		if(identifier.equalsIgnoreCase("COLUMN"))
		{
			return this.getColumn(index);
		}
		else
		{
			return null;
		}
	}
}